package uk.co.q3c.deplan.server.unit.service;

import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.client.domain.dag.ChangeInvalidException;
import uk.co.q3c.deplan.client.domain.dag.CycleDetectedException;
import uk.co.q3c.deplan.client.domain.resource.IndividualResource;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.client.domain.task.BaseTask;
import uk.co.q3c.deplan.client.service.PlanManager;
import uk.co.q3c.deplan.client.service.ServiceFactory;
import uk.co.q3c.deplan.server.Server;
import uk.co.q3c.deplan.server.util.Q3Calendar;

/**
 * {@link PlanManager}
 * 
 * @author DSowerby 26 Nov 2008
 * 
 */
@Test
public class PlanManager_UT {
	PlanManager pm;
	ServiceFactory connection;

	@BeforeMethod
	public void beforeMethod() {
		connection = Server.connection();
		pm = new PlanManager(connection);
	}

	@AfterMethod
	public void afterMethod() {
	}

	public void horizonAndEndDate() {
		// default
		Assert.assertEquals(pm.getHorizon(), 365);
		Assert.assertNotNull(pm.getPlanEnd());
		Q3Calendar today = new Q3Calendar();

		// start with 0 difference
		pm.setHorizon(1);
		Q3Calendar end = new Q3Calendar(pm.getPlanEnd()
				.getTime());
		long daysDiff = today.daysDifferenceTo(end);
		Assert.assertEquals(daysDiff, 0);

		// attempt a negative horizon
		pm.setHorizon(-1);
		end = new Q3Calendar(pm.getPlanEnd()
				.getTime());
		daysDiff = today.daysDifferenceTo(end);
		Assert.assertEquals(daysDiff, 0);

		int testDiff = 1;
		pm.setHorizon(testDiff);
		end = new Q3Calendar(pm.getPlanEnd()
				.getTime());
		daysDiff = today.daysDifferenceTo(end);
		Assert.assertEquals(daysDiff, testDiff - 1);

		testDiff = 365;
		pm.setHorizon(testDiff);
		end = new Q3Calendar(pm.getPlanEnd()
				.getTime());
		daysDiff = today.daysDifferenceTo(end);
		Assert.assertEquals(daysDiff, testDiff - 1);

	}

	/**
	 * No longer required. PlanProfileEntry does not need range set.
	 * 
	 * @throws CycleDetectedException
	 * @throws ChangeInvalidException
	 * @throws PersistenceReferenceException
	 */
	// @Test(dependsOnMethods = "horizonAndEndDate")
	@Test(enabled = false)
	public void dateRange() throws CycleDetectedException, ChangeInvalidException, PersistenceReferenceException {
		BaseTask p = new BaseTask(connection);
		p.setName("project");
		BaseTask pt = new BaseTask(connection);
		pt.setParentTask(p);
		pt.setName("wiggly thing");
		IndividualResource ir = new IndividualResource(connection);
		ir.setName("dsowerby");
		pt.assignResource(ir, 1);

		// pm.setHorizon(5);
		// PlanProfileEntry trp = pt.getProfile(ir);
		// trp.setPlanRange(pm.getCurrentDate(), pm.getPlanEnd());
		// Assert.assertEquals(trp.getPlanStart(), Q3DateUtil.dateOnly_date(pm.getCurrentDate()));
		// Assert.assertEquals(trp.getPlanEnd(), Q3DateUtil.dateOnly_date(pm.getPlanEnd()));
		// Assert.assertEquals(trp.getPlanRange(), 5);

	}
}
